#!/usr/bin/python3
# coding=utf-8

import json
import sys
import os
import time
###################################################################
## 如前端输入
## {
##	"instance":"192.168.1.101",
##	"thresh":"10000",
##	"diag_time":"10",
##	"diag_thread":"10002"
##	"诊断应用":"myApp"
## }
## '{"instance":"192.168.1.101", "diag_time":10, "thresh":10000, "diag_thread":10002}'
## 解析参数方法:
## sysak schedmoni 10000  -s 10 -t 10002
##  10000 thresh, 捕获延迟10000 us的事件
##  -s 10 诊断时间10秒
##  -t 10002 针对线程10002进行监控
##  -c "myApp" 针对应用myApp进行监控
##
######################################################################
class Param(dict):
    def __missing__(self,key):
        sys.stderr.write("入参检查失败，没有检查到入参'%s'"%key)
        exit(1)

args = Param(json.loads(sys.argv[1]))

result = {}
result['commands'] = []

cmd0 = {}
cmd0['instance'] = args["instance"]
arg_time = args["diag_time"]
if arg_time:
	arg_time = '-s ' + str(arg_time)
arg_thresh = args["thresh"]
arg_tid = args["diag_thread"]
if arg_tid:
	arg_tid = '-t ' + str(arg_tid)

arg_app = args["诊断应用"]
if arg_app:
	arg_app = '-c ' + str(arg_app)

dump_log_cmd = "cat /var/log/sysak/schedmoni/runslow.log;\
    echo \"\";cat /var/log/sysak/schedmoni/nosched.log;"
schedmoni_cmd = "sysak schedmoni "+str(arg_tid)+" "+str(arg_app)+" "+str(arg_thresh)+" "+str(arg_time)
cmd0['cmd'] = schedmoni_cmd+" && "+dump_log_cmd

result['commands'].append(cmd0)

data = json.dumps(result)
print(data)
